package com.sdhs.projectstart.service.impl;


import com.sdhs.businessfile.domain.BusinessFile;
import com.sdhs.businessfile.service.IBusinessFileService;
import com.sdhs.common.enums.BusinessTypeEnum;
import com.sdhs.common.utils.SecurityUtils;
import com.sdhs.projectstart.domain.ProjectDept;
import com.sdhs.projectstart.domain.ProjectScheme;
import com.sdhs.projectstart.mapper.ProjectSchemeMapper;
import com.sdhs.projectstart.service.IProjectSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 施工组织方案信息 服务层
 *
 * @author ww
 */
@Service
public class ProjectSchemeServiceImpl implements IProjectSchemeService {
    @Autowired
    private ProjectSchemeMapper projectSchemeMapper;

    @Autowired
    private IBusinessFileService businessFileService;
    /**
     * 查询list
     *
     * @param proScheme 查询条件
     * @return 数组
     */
    @Override
    public List<ProjectScheme> selectProjectSchemeList(ProjectScheme proScheme) {
        return projectSchemeMapper.selectProjectSchemeList(proScheme);
    }

    /**
     * 查询对象
     *
     * @param id 参数ID
     * @return 对象信息
     */
    @Override
    public ProjectScheme selectProjectSchemeById(Long id) {
        ProjectScheme projectScheme = projectSchemeMapper.selectProjectSchemeById(id);
        List<BusinessFile> businessFiles = businessFileService.listByBusinessTypeAndId(BusinessTypeEnum.GC_PROJECT_CONSTRUCTION_ORGANIZATION_SCHEME.getCode(), String.valueOf(id));
        projectScheme.setFileList(businessFiles);
        return projectScheme;
    }

    /**
     * 新增保存信息
     *
     * @param proScheme 对象信息
     * @return 结果
     */
    @Override
    public ProjectScheme insertProjectScheme(ProjectScheme proScheme) {
        proScheme.setApplicant(SecurityUtils.getNickName());
        projectSchemeMapper.insertProjectScheme(proScheme);
        handler(proScheme.getId(), proScheme);
        return proScheme;
    }

    /**
     * 修改保存信息
     *
     * @param proScheme 对象信息
     * @return 结果
     */
    @Override
    public int updateProjectScheme(ProjectScheme proScheme) {
        int i = projectSchemeMapper.updateProjectScheme(proScheme);
        handler(proScheme.getId(), proScheme);
        return i;
    }

    /**
     * 批量删除项信息
     *
     * @param ids ids
     * @return 结果
     */
    @Override
    public int deleteProjectSchemeByIds(Long[] ids) {
        return projectSchemeMapper.deleteProjectSchemeByIds(ids);
    }

    /**
     * 保存合同附件
     */
    public void handler(Long id, ProjectScheme proScheme) {
        // 保存合同附件
        businessFileService.saveFileBatch(BusinessTypeEnum.GC_PROJECT_CONSTRUCTION_ORGANIZATION_SCHEME.getCode(), String.valueOf(id), proScheme.getFileList());
    }
}
